﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Services.Description;
using iPoint.Server.Configuration;


namespace Golos.Web
{
    public class Settings
    {
        public const string SETTINGS_FILE = "config";

        public IPEndPoint UsersEndPoint { get; private set; }
        public IPEndPoint PollsEndPoint { get; private set; }
        public IPEndPoint TagsEndPoint { get; private set; }
        public int TokenLifeTime { get; private set; }
        public int CookieLifeTime { get; private set; }
        public int VkAppId { get; private set; }
        public string VkSecretKey { get; private set; }
        public long FbAppId { get; private set; }
        public string FbSecretKey { get; private set; }
        public string TwAppKey { get; private set; }
        public string TwSecretKey { get; private set; }
        public string MailRuAppKey { get; private set; }
        public string MailRuSecretKey { get; private set; }

        public static Settings ReadFromFile()
        {
            SettingsManager settingsManager = SettingsManager.ReadFromSettingsFile(SETTINGS_FILE);

            int tokenLifeTime = settingsManager.GetInt32Value("TokenLifeTime");
            int cookieLifeTime = settingsManager.GetInt32Value("TokenLifeTime");
            IPEndPoint usersEndPoint = settingsManager.GetEndPointValue("UsersEndPoint");
            IPEndPoint pollsEndPoint = settingsManager.GetEndPointValue("PollsEndPoint");
            IPEndPoint tagsEndPoint = settingsManager.GetEndPointValue("TagsEndPoint");
            int vkAppId = settingsManager.GetInt32Value("VkAppId");
            string vkSecretKey = settingsManager.GetValue("VkSecretKey");
            long fbAppId = settingsManager.GetInt64Value("FbAppId");
            string fbSecretKey = settingsManager.GetValue("FbSecretKey");
            string twAppKey = settingsManager.GetValue("TwAppKey");
            string twSecretKey = settingsManager.GetValue("TwSecretKey");
            string mailRuAppKey = settingsManager.GetValue("MailRuAppKey");
            string mailRuSecretKey = settingsManager.GetValue("MailRuSecretKey");

            return new Settings(usersEndPoint, pollsEndPoint, tagsEndPoint, vkAppId, vkSecretKey, fbAppId, fbSecretKey, twAppKey, twSecretKey, tokenLifeTime, mailRuAppKey, mailRuSecretKey, cookieLifeTime);
        }

        private Settings(IPEndPoint usersEndPoint, IPEndPoint pollsEndPoint, IPEndPoint tagssEndPoint, int vkAppId, string vkSecretKey, long fbAppId, string fbSecretKey, string twAppKey, string twSecretKey, int tokenLifeTime, string mailRuAppKey, string mailRuSecretKey, int cookieLifeTime)
        {
            UsersEndPoint = usersEndPoint;
            PollsEndPoint = pollsEndPoint;
            TagsEndPoint = tagssEndPoint;
            VkAppId = vkAppId;
            VkSecretKey = vkSecretKey;
            FbAppId = fbAppId;
            FbSecretKey = fbSecretKey;
            TwAppKey = twAppKey;
            TwSecretKey = twSecretKey;
            TokenLifeTime = tokenLifeTime;
            CookieLifeTime = cookieLifeTime;
            MailRuAppKey = mailRuAppKey;
            MailRuSecretKey = mailRuSecretKey;
        }
    }
}
